<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/4/3
 * Time: 09:03
 */

namespace app\index\logic;


use app\common\model\Page;

class AnswerLogLogic
{
    /**
     * 获取答题记录
     * @param $data
     * @return mixed
     */
    public function getAnswerLogList($data){
        $map = [];
        if(!isset($data['page_size'])){
            $data['page_size'] = 10;
        }
        if(!isset($data['page_num'])){
            $data['page_num'] = 1;
        }
        if(isset($data['red_envelope_id'])){
            $map['red_envelope_id'] = $data['red_envelope_id'];
        }
        if(isset($data['status'])){
            $map['status'] = $data['status'];
        }
        $result = $this->queryByMap($map,$data['page_num'],$data['page_size']);
        foreach ($result['list'] as $key => &$value) {
            $user = model('User')->where(array('id' => $value['user_id']))->field('avatar,nickname')->find();
            $value['avatar'] = $user['avatar'];
            $value['nickname'] = $user['nickname'];
        }
        unset($value);
        return $result;
    }

    private function queryByMap($map,$page_num,$page_size){
        $startno = ($page_num - 1) * $page_size;
        $map['is_pay'] = 1;
        $list = model('AnswerLog')
            ->where($map)
            ->limit($startno,$page_size)
            ->order('id','desc')
            ->field('topic_ids',true)
            ->select();
        $count = model('AnswerLog')
            ->where($map)
            ->count();
        $result['list'] = $list;
        $page = new Page($page_num,$count,$page_size);
        $result['page'] = $page;
        return $result;
    }
}